import Vue from 'vue'
import $ from 'jquery'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
// 导入全局样式表
import './assets/css/global.css'
//全局mixin
import mixin from './mixin/mixin'
Vue.mixin(mixin);

Vue.config.productionTip = false
Vue.use(ElementUI);


/** 
* @desc :时间选择器 - 多种输入格式
* @date :
* @params :
* @author : liangdecheng
**/ 
Vue.directive('time', {
  inserted: function (el, binding, vnode) {
    const { value: _obj } = binding
    const { context: that, data } = vnode
    const { expression: key } = data.model

    if (that && that._isVue) {
      const $this = $($(el).children('input')[0])

      $this.on('change', function () {
        let value = $this.val()
        value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3') // 格式化输入格式

        const time = value && value.constructor == String ? new Date(value) : value  // 转换时间格式
        let keys = key.split('.')

        // 自定义赋值
        if (_obj) {
          const { keys: keyList } = _obj
          keys = keyList
        }

        if (keys && keys.length >= 2) {
          const [key1, key2, key3, key4] = keys

          if (key4) {
            that[key1][key2][key3][key4] = time
          } else if (key3) {
            that[key1][key2][key3] = time
          } else {
            that[key1][key2] = time
          }
        } else {
          that[key] = time
        }
      })
    }
  }
})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
